package com.chengqj;

import com.chengqj.dao.UserDO;
import com.chengqj.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Copyright © 2020年 com.qxkj. All rights reserved.
 *
 * @author chengqj
 * spring 只有在抛出的异常为运行时 unchecked 异常时才回滚该事务
 * 异常一旦被捕获不抛出了，事务就不会生效
 * version 1.0.0 (接口必须带上)
 * @date 2020/11/5 16:43
 */
@Service
public class UserServiceTryCatchException {
    @Autowired
    private UserRepository userRepository;


    /**
     * 默认配置下，spring 只有在抛出的异常为运行时 unchecked 异常时才回滚该事务
     * 异常一旦被捕获不抛出了，事务就不会生效
     *
     * @param userDO
     * @return
     */
    @Transactional
    public UserDO createTryCatchException(UserDO userDO) {
        try {
            userRepository.save(userDO);
            int i = 1 / 0;
        } catch (RuntimeException e) {
            System.out.println(e.getMessage());
        }
        return userDO;
    }

}
